package mn.idax.exchange.otc.exception;

import com.alibaba.fastjson.JSON;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.enums.response.SystemStatusResponseEnum;
import mn.idax.exchange.otc.util.BaseModel;
import mn.idax.exchange.otc.util.I18nUtil;
import mn.idax.exchange.otc.util.LogUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

/**
 * @Auther: dison
 * @Date: 2018/7/27 11:16
 * @Description: 全局异常捕捉类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     *
     * 功能描述: 全局异常
     * @param: e 异常类
     * @return: ResponseData 公共实体响应内容
     * @auther: dison
     * @date: 2018/7/31 9:42
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseData handle(Exception e){
        LogUtils.error(GlobalExceptionHandler.class, "Exceptionhandle",e);
        // 错误信息返回
        return BaseModel.makeI18nResponseData(SystemStatusResponseEnum.SYSTEM_ERROR.getCode());
    }

    /**
     *
     * 功能描述: 自定义异常类捕捉
     * @param: e 自定义异常类型
     * @return:  ResponseData 公共实体响应内容
     * @auther: dison
     * @date: 2018/7/31 9:42
     */
    @ExceptionHandler(value = IdaxException.class)
    @ResponseBody
    public ResponseData handleIdaxException(IdaxException e){
        LogUtils.warn(GlobalExceptionHandler.class, " handleIdaxException " + ExceptionUtils.getStackTrace(e));
        // 国际化
        return  BaseModel.makeI18nResponseData(e.getCode());
    }

    @ExceptionHandler(value = DynamicException.class)
    @ResponseBody
    public ResponseData handleDynamicException(DynamicException e){
        // 国际化
        LogUtils.warn(GlobalExceptionHandler.class, " handleDynamicException " + ExceptionUtils.getStackTrace(e));
        ResponseData responseData = (ResponseData) I18nUtil.replaceLangeage(JSON.toJSONString(ResponseData.error(e.getCode(), null)), e.getBtc(), e.getEth(), e.getUsdt());
        return  responseData;
    }

    /**
     *
     * 功能描述: 参数校验异常捕捉
     *         1、前期考虑将所有非数据库层面异常统一状态码
     *         2、后续考虑是否不同参数响应不同状态码
     *         3、考虑响应国际化
     * @param: e 参数校验异常类
     * @return: ResponseData 公共实体响应内容
     * @auther: dison
     * @date: 2018/7/31 9:41
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseData handleIllegalParamException(MethodArgumentNotValidException e) {

        LogUtils.warn(GlobalExceptionHandler.class, "handleIllegalParamException :" + ExceptionUtils.getStackTrace(e));

        String code = Optional.ofNullable(e.getBindingResult().getAllErrors())
                .map(u -> u.get(0))
                .map(DefaultMessageSourceResolvable :: getDefaultMessage)
                .orElse(SystemStatusResponseEnum.FAIL.getCode().toString());
        // 错误信息返回
        return BaseModel.makeI18nResponseData(Integer.valueOf(code));
    }
}
